<?php
require('functions/page_load.php');

$settings_nav = true;

if(isset($_GET['delete'])){

	$sql = "UPDATE subscriptions SET deleted  = 1 WHERE id = '".mysql_real_escape_string($_GET['delete'])."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$results = '<div class="success_result">Subscription deleted successfully.</div>';

}

if($_SERVER['REQUEST_METHOD'] == 'POST'){

	if(isset($_GET['id'])){
	
		//Change By Vyas Ishan Added the the field status condition status = '1'	24 May 2012
		if (array_key_exists('status',$_POST)) {
			$subscriptionStatus = "1";
		} else {
			$subscriptionStatus = "0";
		}
	
		//Change By Vyas Ishan Added the the field status condition status = '1'	24 May 2012
		$sql = "UPDATE subscriptions SET 
					radius = '".mysql_real_escape_string($_POST['radius'])."',
					status = '".$subscriptionStatus."',
					cost = '".mysql_real_escape_string($_POST['cost'])."' 
				WHERE id = '".mysql_real_escape_string($_GET['id'])."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$results = '<div class="success_result">Subscription updated successfully.</div>';	
	
	}
	
	else{
			
			
			
		
	$sql = "INSERT INTO subscriptions 
		SET radius = '".mysql_real_escape_string($_POST['radius'])."', 
		cost = '".mysql_real_escape_string($_POST['cost'])."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$results = '<div class="success_result">Subscription created successfully.</div>';
	
	}

}


	//Change By Vyas Ishan Added the the field status condition status = '1'	24 May 2012
	$sql = "SELECT id, name, radius, cost,status  FROM subscriptions ORDER BY id";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$rows = mysql_num_rows($query);
	
	if($rows > 0){
	
	$content = '<table width="100%" cellpadding="0" cellspacing="0" class="cms_table"><thead><tr><th align="left">Name</th><th align="left">Radius</th><th align="left">Cost</th><th width="60">Edit</th></tr></thead><tbody>';
	
		while($rs=mysql_fetch_assoc($query)){
		
		$content.='<tr><td align="left">'.$rs['name'].'</td><td align="left">'.$rs['radius'].' miles</td><td align="left">&pound;'.$rs['cost'].'</td><td><a href="?id='.$rs['id'].'" title="Edit"><img src="images/edit.png" alt="edit" /></a></td></tr>';
		
		}
		
	$content.='</tbody></table>';
		
	}
	
	else{
	
	$content = '<p style="float: left; width: 100%; text-align: center;">There are currently no subscriptions.</p>';
	
	}
	
if(isset($_GET['id'])){
	//Change By Vyas Ishan Added the the field status condition status = '1'	24 May 2012
$sql = "SELECT radius, cost,status FROM subscriptions WHERE id = '".mysql_real_escape_string($_GET['id'])."'";
$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
$rs = mysql_fetch_assoc($query);

}

require('includes/header.php');
?>

<div class="side_container">
<h2 class="container_header">Navigation</h2>

<ul>
<li><a href="banners.php">Banners</a></li>
<li><a href="trades.php">Trades</a></li>
<li><a href="directory_categories.php">Directory Categories</a></li>
<li><a href="subscriptions.php">Subscriptions</a></li>
<li><a href="sms_packages.php">SMS Packages</a></li>
</ul>

</div>

<div class="main_container_wrapper">

	<div class="main_container">
	<h2 class="container_header">Subscriptions</h2>
	
	<?php echo $results; ?>
	
	<?php if(isset($_GET['id'])){ ?>
	
	<form method="post" action="">

			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="radius">Radius:</label></div>
			<div class="form_row_right"><input type="text" name="radius" id="radius" class="textbox" value="<?php echo $rs['radius']; ?>" style="width: 80px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="cost">Cost:</label></div>
			<div class="form_row_right"><input type="text" name="cost" id="cost" class="textbox" value="<?php echo $rs['cost']; ?>" style="width: 80px;" /><span style="margin-left: 10px;">Only use numbers and decimals, do not include a pound sign</span></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="cost">Status:</label></div>
			<div class="form_row_right"><input type="checkbox" <?php echo ($rs['status'] == '1')?"checked":""; ?> name="status" id="status" class="" value="1" style="width: 80px;" /><span style="margin-left: 10px;">Status of the subscription whether active or deactive</span></div>
			</div>
			
			
			<div class="form_row_wrapper">
			<div class="form_row_left">&nbsp;</div>
			<div class="form_row_right"><input type="image" src="images/save_button.png" alt="Save" /></div>
			</div>
			
			</form>
			
	<?php } ?>

	<?php echo $content; ?>

	</div>

</div>
	
<?php
	require('includes/footer.php');
?>